package com.greateffect.littlebud.lib.popup;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

/**
 * Created by ZengCS on 2019/4/24.
 * E-mail:zengcs@vip.qq.com
 * Add:成都市天府软件园E3
 */
public class SimplePopupHelper {
    private Context mContext;
    private PopupWindow mPopupWindow;

    public SimplePopupHelper(Context context) {
        mContext = context;
    }

    public void initPopupWindow(int width, int layoutId) {
        View popupView = LayoutInflater.from(mContext).inflate(layoutId, null, false);

        // 初始化
        mPopupWindow = new PopupWindow();
        mPopupWindow.setContentView(popupView);
        mPopupWindow.setWidth(width);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.update();
    }

    public void showPopupWindow(View view, int xOff, int yOff) {
        mPopupWindow.showAsDropDown(view, xOff, yOff);
    }

    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing())
            mPopupWindow.dismiss();
    }
}
